#include "chooselevelscene.h"
#include<QMenuBar>
#include<QPainter>
#include "mypushbutton.h"
#include<QDebug>
#include<QTimer>
#include<QLabel>
#include<QSound>

ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    //选择关卡按钮音效
        QSound *chooseSound = new QSound(":/res/TapButtonSound.wav",this);
    //返回按钮音效
        QSound *backSound = new QSound(":/res/BackButtonSound.wav",this);

    //设置窗口固定大小
        this->setFixedSize(320,588);
        //设置图标
        this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
        //设置标题
        this->setWindowTitle("选择关卡");

        //创建菜单栏
        QMenuBar * bar = menuBar();
        this->setMenuBar(bar);
        //创建开始菜单
        QMenu * startMenu = bar->addMenu("开始");
        //创建按钮菜单项
        QAction * quitAction = startMenu->addAction("退出");
        //点击退出 退出游戏
        connect(quitAction,&QAction::triggered,[=](){this->close();});
        //返回按钮
            MyPushButton * backBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
            backBtn->setParent(this);
                backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

                connect(backBtn,&MyPushButton::clicked,[=](){
                    backSound->play();
                    qDebug()<<"返回到主场景";
                    //发送点击返回按钮的信号
                    QTimer::singleShot(500,this,[=]{
                    emit this->ChooseSceneBack();
                    });
                });
                pScene=NULL;
           //创建关卡按钮
              for(int i = 0 ; i < 20;i++)
              {
                    MyPushButton * menuBtn = new MyPushButton(":/res/LevelIcon.png");
                    menuBtn->setParent(this);
                    menuBtn->move(25 + (i%4)*70 , 130+ (i/4)*70);

                    connect(menuBtn,&MyPushButton::clicked,[=](){
                        chooseSound->play();
                        qDebug()<<"您选择的是第"<<i+1<<"关";
                        this->pScene=new PlayScene(i+1);
                        this->pScene->show();
                        this->hide();
                        //监听第三个场景的返回按钮信号
                        connect(this->pScene,&PlayScene::ChooseSceneBack,[=](){
                            this->pScene->hide();
                                  this->show();
                            delete this->pScene;

                              this->pScene = NULL;
                                        });

                    });
                    //按钮上显示的文字
                            QLabel * label = new QLabel;
                            label->setParent(this);
                            label->move(25 + (i%4)*70 , 130+ (i/4)*70);
                            label->setFixedSize(menuBtn->width(),menuBtn->height());
                            label->setText(QString::number(i+1));
                            label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); //设置居中
                            label->setAttribute(Qt::WA_TransparentForMouseEvents);  //鼠标事件穿透,在按钮上盖了文字，无法点击

              }


}
void ChooseLevelScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load("://res/8cff0a4b5091454226299cb92aea2bac.jpg");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

     //加载标题
    pix.load(":/res/Title.png");
    painter.drawPixmap( (this->width() - pix.width())*0.5,30,pix);
}

